package com.heima.home.mapper;

import com.heima.home.entity.Users;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM users WHERE username = #{username}")
    Users findByUsername(String username);

    @Select("SELECT * FROM users WHERE email = #{email}")
    Users findByEmail(String email);

    @Select("SELECT * FROM users WHERE id = #{id}")
    Users findById(Integer id);

    @Insert("INSERT INTO users(username, password, email, user_type, status, created_at, updated_at) " +
            "VALUES(#{username}, #{password}, #{email}, 1, 1, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Users user);

    @Update("UPDATE users SET avatar = #{avatar}, updated_at = NOW() WHERE id = #{id}")
    int updateAvatar(@Param("id") Integer id, @Param("avatar") String avatar);
    @Update("UPDATE users SET username = #{username},password=#{password}, email = #{email}, phone = #{phone}, real_name = #{realName},id_card=#{idCard},gender=#{gender},birthday=#{birthday},updated_at=now() " +
            "WHERE id = #{id}")
    int updateUserInfo(Users user);

}